//package io.renren.modules.webapi.controller;
//
//import cn.hutool.json.JSONObject;
//import io.netty.handler.codec.http.HttpHeaders;
//import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
//import io.netty.handler.timeout.IdleStateEvent;
//import io.renren.modules.webapi.thread.CproThread;
//import io.renren.modules.webapi.thread.FcopyThread;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.yeauty.annotation.*;
//import org.yeauty.pojo.Session;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
//
////@ServerEndpoint(path="/ws/file/{arg}",port="${netty-websocket.port}")
////@Component
////public class FileNettyWebsocket {
////
////    private static final Logger log = LoggerFactory.getLogger(FileNettyWebsocket.class);
////
////    private static final String successFlag = "OK";
////
////
////    /**
////     * 用户channel映射关系
////     */
////    public static final ConcurrentHashMap<String, Session> userChannelMap = new ConcurrentHashMap<>();
////
////    /**
////     * 当有新的连接进入时，对该方法进行回调
////     * 可做简单的鉴权
////     * @param session
////     * @param headers
////     * @param req
////     * @param reqMap
////     * @param arg
////     * @param pathMap
////     */
////    @BeforeHandshake
////    public void handshake(Session session, HttpHeaders headers,
////                          @RequestParam String req,
////                          @RequestParam MultiValueMap reqMap,
////                          @PathVariable String arg,
////                          @PathVariable Map pathMap) {
////        session.setSubprotocols("stomp");
////    }
////
////    /**
////     * 当有新的WebSocket连接完成时，对该方法进行回调
////     *
////     * @param session
////     * @param headers
////     * @param req
////     * @param reqMap
////     * @param arg
////     * @param pathMap
////     * @throws IOException
////     */
////    @OnOpen
////    public void onOpen(Session session, HttpHeaders headers,
////                       @RequestParam String req,
////                       @RequestParam MultiValueMap reqMap,
////                       @PathVariable String arg,
////                       @PathVariable Map pathMap){
////        log.info("new File connection: " + arg);
////        // 模拟获取到的userId
////        String userId = arg;
////        if (userChannelMap.get(userId) == null) {
////            userChannelMap.put(userId, session);
////        }
////        log.info("当前在线人数：" + userChannelMap.size());
////    }
////
////    /**
////     * 当有WebSocket连接关闭时，对该方法进行回调
////     *
////     * @param session
////     * @param arg
////     * @throws IOException
////     */
////    @OnClose
////    public void onClose(Session session, @PathVariable String arg) {
////        log.info("one connection closed");
////        if (userChannelMap != null) {
////            if(userChannelMap.get(arg) != null){
////                userChannelMap.remove(arg);
////            }
////        }
////        session.close();
////        log.info("当前在线人数：" + userChannelMap.size());
////    }
////
////    /**
////     * 当有WebSocket抛出异常时，对该方法进行回调
////     *
////     * @param session
////     * @param throwable
////     */
////    @OnError
////    public void onError(Session session, @PathVariable String arg, Throwable throwable) {
////        log.error("one connection error:" + throwable.getMessage());
////        this.onClose(session,arg);
////    }
////
////    /**
////     * 当接收到字符串消息时，对该方法进行回调
////     *
////     * @param session
////     * @param message
////     */
////    @OnMessage
////    public void onMessage(Session session, String message) {
////        log.info(">o< 接收到消息: {}"+message);
////        JSONObject json = new JSONObject(message);
////        String type = json.get("type").toString();
////        switch (type) {
////            case "kill":
////                Long cid =  Long.valueOf(String.valueOf(json.get("cid"))).longValue();
////                CproThread.killThread(cid);
////                Long fid =  Long.valueOf(String.valueOf(json.get("fid"))).longValue();
////                FcopyThread.killThread(fid);
////                break;
////            case "file":
////                String resource = "E:\\ygg\\test\\cpjar\\filecopy\\target\\file\\1" + File.separator + "jnpf-web";
////                String target = "E:\\ygg\\test\\cpjar\\filecopy\\target\\file\\2";
////                copyFileHandle(resource,target,session);
////                break;
////            default:
////                break;
////        }
////       // if("PING".equals(message)){
//////        broadcast(message);
////        //}
////    }
////
////    /**
////     * 当接收到二进制消息时，对该方法进行回调
////     * @param session
////     * @param bytes
////     */
////    @OnBinary
////    public void onBinary(Session session, byte[] bytes) {
////        session.sendBinary(bytes);
////    }
////
////    /**
////     * 当接收到Netty的事件时，对该方法进行回调
////     *
////     * @param session
////     * @param evt
////     */
////    @OnEvent
////    public void onEvent(Session session, Object evt) {
////        if (evt instanceof IdleStateEvent) {
////            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
////            switch (idleStateEvent.state()) {
////                case READER_IDLE:
////                    log.error("Client: " + session.channel().id() + " READER_IDLE 读超时");
////                    break;
////                case WRITER_IDLE:
////                    log.error("Client: " + session.channel().id() + " WRITER_IDLE 写超时");
////                    break;
////                case ALL_IDLE:
////                    log.error("Client: " + session.channel().id() + " ALL_IDLE 总超时");
////                    break;
////                default:
////                    break;
////            }
////        }
////    }
////
////
////    /**
////     * 单点推送给某个人
////     *
////     * @param session
////     * @param msg
////     */
////    public static final void sendMessageToUser(Session session, String msg) {
////        if(session.isOpen() && session.isActive()){
////            TextWebSocketFrame tws = new TextWebSocketFrame(msg);
////            session.sendText(tws);
////
////            switch (msg) {
////                case "file":
////
////                    break;
////                default:
////                    break;
////            }
////        }
////    }
////
////    /**
////     * 广播给所有在线的人
////     *
////     * @param msg
////     */
////    public static final void broadcast(String msg) {
////        if (userChannelMap != null) {
////            userChannelMap.forEach((key, value) -> sendMessageToUser(value, msg));
////        }
////    }
////
////    public void copyFileHandle(String resource, String target, Session session) {
////        try {
////            File resourceFile = new File(resource);
////            if (!resourceFile.exists()) {
////                throw new Exception("源目标路径：[" + resource + "] 不存在...");
////            }
////            File targetFile = new File(target);
////            if (!targetFile.exists()) {
////                throw new Exception("存放的目标路径：[" + target + "] 不存在...");
////            }
////
////            FcopyThread fcopyThread = new FcopyThread("CopyThread",resource,target);
////            Thread cfile = new Thread(fcopyThread);
////            cfile.start();
////
////            CproThread cproThread = new CproThread("CproThread",resource, target + File.separator + "jnpf-web",session);
////            Thread cpro = new Thread(cproThread);
////            cpro.start();
////
////            Map<String,Object> params = new HashMap<>();
////            params.put("fId",cfile.getId());
////            params.put("cid",cpro.getId());
////            sendMessageToUser(session,"拷贝线程开启： " +params.toString() );
////
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
////    }
////}
